<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class End_user extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->model ( 'function_model' );
		$this->load->model ( 'MChain' );
	//	$this->output->enable_profiler(true);
	}
	public function index() {
		$data ['title'] = 'Main';
		$this->load->view ( 'header', $data );
		$this->load->view ( "main", $data );
		$this->load->view ( 'footer', $data );
	}
	public function end_user_register() {
		$data ['title'] = 'Signup';
		$data['option_province'] = $this->MChain->getprovince();
		$this->load->view ( 'header', $data );
		$this->load->view ( "end_user_register", $data );
		$this->load->view('footer',$data);
	}
	public function welcome() {
		$data ['title'] = 'Welcome';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'welcome', $data );
		$this->load->view ( 'footer', $data );
	}
	public function main() {
		$data ['title'] = 'Signup';
		$data['option_province'] = $this->MChain->getprovince_user();
		$this->load->view ( 'header', $data );
		$this->load->view ( "end_user_register", $data );
		
		// $this->load->view('footer',$data);
	}
	
	public function thank_add() {
		$data ['title'] = 'Thank';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'end_user_register_suscess', $data );
		// $this->load->view('footer',$data);
	}
	public function registration() {
	
		
			$this->function_model->add_user ();
	//		$id = $this->db->insert_id ();
	//		$member = $this->function_model->get_member_by_id ( $id );
	//		$this->send_email ( "activate", $member );
			$this->thank_add ();
		
	}
	
	function registration_view(){
		$this->load->library ( 'form_validation' );
		// field name, error message, validation rules
		$this->form_validation->set_rules ( 'name', 'Name', 'trim|required|xss_clean|valid_string|min_length[2]|max_length[100]' );
		$this->form_validation->set_rules ( 'email', 'Your Email', 'trim|required|valid_email|min_length[5]|max_length[100]' );
		$this->form_validation->set_rules ( 'tel_number1', 'Telephone Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'tel_number2', 'Telephone Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'tel_number3', 'Telephone Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'fax1', 'Fax Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'fax2', 'Fax Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'fax3', 'Fax Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'address', 'Address', 'trim|required|xss_clean' );
		$this->form_validation->set_rules ( 'province', 'Province', 'trim|required' );
		$this->form_validation->set_rules ( 'district', 'District', 'trim|required' );
		$this->form_validation->set_rules ( 'meet_date', 'Meet Date', 'trim|required' );
		
		if ($this->form_validation->run () == FALSE) {
			$this->main ();
		}else{
		$data = array (
				"name" => $this->input->post ( 'name' ),
				"email" => $this->input->post ( 'email' ),
				"tel_number1" => $this->input->post ( 'tel_number1' ),
				"tel_number2" => $this->input->post ( 'tel_number2' ),
				"tel_number3" => $this->input->post ( 'tel_number3' ),
				"fax1" => $this->input->post ( 'fax1' ),
				"fax2" => $this->input->post ( 'fax2' ),
				"fax3" => $this->input->post ( 'fax3' ),
				"province" => $this->input->post ( 'province' ),
				"district" => $this->input->post ( 'district' ),
				"meetdate" => $this->input->post ( 'meet_date' ),
				"address" => $this->input->post ( 'address' )
		);
		$data['get_province'] = $this->function_model->select_province($this->input->post ( 'province' ));
		$data['get_district'] = $this->function_model->select_district($this->input->post ( 'district' ));
		$this->load->view ( 'header', $data );
		$this->load->view ( "end_user_register_view", $data );
		}
		
	}
	
	function send_email($type, $member) {
		$config = array (
				'protocol' => 'smtp',
				'smtp_host' => 'ssl://smtp.googlemail.com',
				'smtp_port' => 465,
				'smtp_user' => 'pemiga.me@gmail.com',
				'smtp_pass' => '14022511mana',
				'mailtype' => 'html',
				'charset' => 'utf-8' 
		);
		
		$this->load->library ( "email", $config );
		$this->email->set_newline ( "\r\n" );
		
		$this->email->from ( 'Team Packs Asia' );
		$this->email->to ( $member->email );
		
		if ($type == "activate") {
			
			$this->email->subject ( 'Register Success !!' );
			
			$data = array (
					"name" => $member->name,
					"email" => $member->email 
			);
			
			$email_message = $this->load->view ( "activate-email-page", $data, true );
			$this->email->message ( $email_message );
		}
		
		$this->email->send ();
	}
	

	function selectdistrict($id = "") {
		$data ['option_district'] = $this->MChain->getdistrict_user ( $id );
		$this->load->view ( 'selectDistrict', $data );
	}
	
}
?>